//% color="#AA278D" iconWidth=50 iconHeight=40
namespace oled12864 {
  //% block="OLED 128*64 initialize I2C address [ADDR]" blockType="command"
  //% ADDR.shadow="number"   ADDR.defl="0x3c"
  export function initOled(param: any) {
    const addr = param.ADDR.code;

    Generator.addInclude('U8g2lib.h', '#include <U8g2lib.h>');
    Generator.addInclude('MPython.h', '#include <MPython.h>');
    Generator.addInclude('DFRobot_Task.h', '#include <DFRobot_Task.h>');
    Generator.addInclude('DFRobot_NeoPixel.h', '#include <DFRobot_NeoPixel.h>');

    Generator.addInclude(`mind_n_ch`, `// 动态变量\n` +
      `volatile float mind_n_ch;\n` +
      `volatile float mind_n_en;\n`
    )

    Generator.addInclude(`u8g2_12864`, `// 创建对象\n` +
      `U8G2_SSD1306_128X64_NONAME_F_HW_I2C u8g2_12864(U8G2_R0, U8X8_PIN_NONE);\n` +
      `DFRobot_NeoPixel neoPixel_33;\n`
    )

    // Generator.addObject(`u8g2_12864`, `U8G2_SSD1306_128X64_NONAME_F_HW_I2C`, `u8g2_12864(U8G2_R0, U8X8_PIN_NONE);`)
    // Generator.addObject(`neoPixel_33`, `DFRobot_NeoPixel`, `neoPixel_33;`)

    Generator.addInclude(`oled12864Begin`, `// 函数声明\n` +
      `void oled12864Begin(uint8_t addr, uint8_t dir);\n` +
      `void oled12864Print(String text, uint8_t x, uint8_t y);\n\n` +
      `void oled12864Begin(uint8_t addr, uint8_t dir) {\n` +
      `  u8g2_12864.setI2CAddress(addr<<1);\n` +
      `  u8g2_12864.begin();\n` +
      `  u8g2_12864.enableUTF8Print();\n` +
      `  u8g2_12864.setFont(u8g2_font_wqy16_t_gb2312);\n` +
      `  u8g2_12864.setFontDirection(dir);\n` +
      `  u8g2_12864.firstPage();\n` +
      `}\n` +
      `void oled12864Print(String text, uint8_t x, uint8_t y) {\n` +
      `  u8g2_12864.setCursor(x, y);\n` +
      `  u8g2_12864.print(text);\n` +
      `  u8g2_12864.nextPage();\n` +
      `}\n`
    )

    Generator.addSetup(`oled12864`, `taskStart();\n` +
      `  mPython.begin();\n` +
      `  oled12864Begin(${addr}, 0);\n` +
      `  neoPixel_33.begin(33, 1);\n` +
      `  neoPixel_33.setBrightness(20);\n` +
      `  neoPixel_33.setRangeColor(0, 0, 0xFFFFFF);\n` +
      `  mind_n_ch = 0; // 左右按键\n` +
      `  mind_n_en = 1; // 确认按键\n` +
      `  taskFree();\n`
    )
  }

  //% block="OLED 128*64 display text [TEXT] in coordinates X:[XV] Y:[YV]" blockType="command"
  //% TEXT.shadow="string"   TEXT.defl="Mind+育伟达"
  //% XV.shadow="range"   XV.params.min=0    XV.params.max=127    XV.defl=8
  //% YV.shadow="range"   YV.params.min=0    YV.params.max=47    YV.defl=16
  export function displayText(param: any) {
    const text = param.TEXT.code;
    const xv = param.XV.code;
    const yv = param.YV.code;
    Generator.addCode(`oled12864Print(${text}, ${xv}, ${yv + 16});`)
  }

  //% block="OLED 128*64 clear screen" blockType="command"
  export function clearScreen() {
    Generator.addCode(`u8g2_12864.clear();`)
  }

  //% block="Key function module" blockType="command"
  export function keyModule() {
    Generator.addCode(`if ((digitalRead(19) == 0) && (mind_n_ch > 0)) {\n` +
      `    delay(10);\n` +
      `    if ((digitalRead(19) == 0) && (mind_n_ch > 0)) {\n` +
      `        mind_n_ch -= 1;\n` +
      `    }\n` +
      `    while (digitalRead(19) == 0) {\n` +
      `        delay(10);\n` +
      `        buzz.redirect(23);\n` +
      `        buzz.freq(147, BEAT_1_4);\n` +
      `    }\n` +
      `    u8g2_12864.clear();\n` +
      `  }\n` +
      `  if ((digitalRead(18) == 0) && (mind_n_ch < 7)) {\n` +
      `    delay(10);\n` +
      `    if ((digitalRead(18) == 0) && (mind_n_ch < 7)) {\n` +
      `        mind_n_ch += 1;\n` +
      `    }\n` +
      `    while (digitalRead(18) == 0) {\n` +
      `        delay(10);\n` +
      `        buzz.redirect(23);\n` +
      `        buzz.freq(147, BEAT_1_4);\n` +
      `    }\n` +
      `    u8g2_12864.clear();\n` +
      `  }\n` +
      `  if ((digitalRead(5) == 0)) {\n` +
      `    delay(10);\n` +
      `    if ((digitalRead(5) == 0)) {\n` +
      `        mind_n_en = !mind_n_en;\n` +
      `    }\n` +
      `    while (digitalRead(5) == 0) {\n` +
      `        delay(10);\n` +
      `        buzz.redirect(23);\n` +
      `        buzz.freq(147, BEAT_1_4);\n` +
      `    }\n` +
      `  }\n` +
      `  while (mind_n_en == 0) {\n` +
      `   delay(10);\n` +
      `   if ((mind_n_en==0)) {\n` +
      `     if ((mind_n_ch==0)) {\n` +
      `       taskStart(loop1);\n` +
      `     }\n` +
      `     if ((mind_n_ch==1)) {\n` +
      `       taskStart(loop2);\n` +
      `     }\n` +
      `     if ((mind_n_ch==2)) {\n` +
      `       taskStart(loop3);\n` +
      `     }\n` +
      `     if ((mind_n_ch==3)) {\n` +
      `       taskStart(loop4);\n` +
      `     }\n` +
      `     if ((mind_n_ch==4)) {\n` +
      `       taskStart(loop5);\n` +
      `     }\n` +
      `     if ((mind_n_ch==5)) {\n` +
      `       taskStart(loop6);\n` +
      `     }\n` +
      `     if ((mind_n_ch==6)) {\n` +
      `       taskStart(loop7);\n` +
      `     }\n` +
      `     if ((mind_n_ch==7)) {\n` +
      `       taskStart(loop8);\n` +
      `     }\n` +
      `   }\n` +
      `   if ((digitalRead(5) == 0)) {\n` +
      `       delay(10);\n` +
      `       if ((digitalRead(5) == 0)){\n` +
      `         mind_n_en = !mind_n_en;\n` +
      `       }\n` +
      `       while (digitalRead(5) == 0) {\n` +
      `         delay(10);\n` +
      `         buzz.redirect(23);\n` +
      `	        buzz.freq(147, BEAT_1_4);\n` +
      `       }\n` +
      `   }\n` +
      ` }\n` +
      ` if ((mind_n_ch==0)) {\n` +
      `   oled12864Print("积木主控", 32, 32);\n` +
      `   oled12864Print("任务一", 40, 56);\n` +
      ` }\n` +
      ` if ((mind_n_ch==1)) {\n` +
      `   oled12864Print("积木主控", 32, 32);\n` +
      `   oled12864Print("任务二", 40, 56);\n` +
      ` }\n` +
      ` if ((mind_n_ch==2)) {\n` +
      `   oled12864Print("积木主控", 32, 32);\n` +
      `   oled12864Print("任务三", 40, 56);\n` +
      ` }\n` +
      ` if ((mind_n_ch==3)) {\n` +
      `   oled12864Print("积木主控", 32, 32);\n` +
      `   oled12864Print("任务四", 40, 56);\n` +
      ` }\n` +
      ` if ((mind_n_ch==4)) {\n` +
      `   oled12864Print("积木主控", 32, 32);\n` +
      `   oled12864Print("任务五", 40, 56);\n` +
      ` }\n` +
      ` if ((mind_n_ch==5)) {\n` +
      `   oled12864Print("积木主控", 32, 32);\n` +
      `   oled12864Print("任务六", 40, 56);\n` +
      ` }\n` +
      ` if ((mind_n_ch==6)) {\n` +
      `   oled12864Print("积木主控", 32, 32);\n` +
      `   oled12864Print("任务七", 40, 56);\n` +
      ` }\n` +
      ` if ((mind_n_ch==7)) {\n` +
      `   oled12864Print("积木主控", 32, 32);\n` +
      `   oled12864Print("任务八", 40, 56);\n` +
      ` }\n` +
      ` if ((mind_n_en==1)) {\n` +
      `   if ((mind_n_ch==0)) {\n` +
      `     taskFree(loop1);\n` +
      `   }\n` +
      `   if ((mind_n_ch==1)) {\n` +
      `     taskFree(loop2);\n` +
      `   }\n` +
      `   if ((mind_n_ch==2)) {\n` +
      `     taskFree(loop3);\n` +
      `   }\n` +
      `   if ((mind_n_ch==3)) {\n` +
      `     taskFree(loop4);\n` +
      `   }\n` +
      `   if ((mind_n_ch==4)) {\n` +
      `     taskFree(loop5);\n` +
      `   }\n` +
      `   if ((mind_n_ch==5)) {\n` +
      `     taskFree(loop6);\n` +
      `   }\n` +
      `   if ((mind_n_ch==6)) {\n` +
      `     taskFree(loop7);\n` +
      `   }\n` +
      `   if ((mind_n_ch==7)) {\n` +
      `     taskFree(loop8);\n` +
      `   }\n` +
      ` }\n`
    )
  }
}
